package com.coffee.core.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NumberValidationUtils {

    private static boolean isMatch(String regex, String orginal) {
        if (orginal == null || orginal.trim().equals("")) {
            return false;
        }
        Pattern pattern = Pattern.compile(regex);
        Matcher isNum = pattern.matcher(orginal);
        return isNum.matches();
    }

    /**
     * 是否是正整数
     *
     * @param orginal
     * @return
     */
    public static boolean isPositiveInteger(String orginal) {
        return isMatch("^\\+{0,1}[1-9]\\d*", orginal);
    }

    /**
     * 是否是负整数
     *
     * @param orginal
     * @return
     */
    public static boolean isNegativeInteger(String orginal) {
        return isMatch("^-[1-9]\\d*", orginal);
    }

    /**
     * 是否是整数
     *
     * @param orginal
     * @return
     */
    public static boolean isWholeNumber(String orginal) {
        return isMatch("[+-]{0,1}0", orginal) || isPositiveInteger(orginal) || isNegativeInteger(orginal);
    }

    /**
     * 是正十进制
     *
     * @param orginal
     * @return
     */
    public static boolean isPositiveDecimal(String orginal) {
        return isMatch("\\+{0,1}[0]\\.[1-9]*|\\+{0,1}[1-9]\\d*\\.\\d*", orginal);
    }

    /**
     * 是负十进制
     *
     * @param orginal
     * @return
     */
    public static boolean isNegativeDecimal(String orginal) {
        return isMatch("^-[0]\\.[1-9]*|^-[1-9]\\d*\\.\\d*", orginal);
    }

    /**
     * 是十进制
     *
     * @param orginal
     * @return
     */
    public static boolean isDecimal(String orginal) {
        return isMatch("[-+]{0,1}\\d+\\.\\d*|[-+]{0,1}\\d*\\.\\d+", orginal);
    }

    /**
     * 满足小数或整数
     * @param orginal
     * @return
     */
    public static boolean isIntegerAndDecimal(String orginal) {
        return isMatch("^(([1-9]{1}\\d*)|(0{1}))(\\.\\d{1,2})?$", orginal);
    }

    /**
     * 是否是实数
     *
     * @param orginal
     * @return
     */
    public static boolean isRealNumber(String orginal) {
        return isWholeNumber(orginal) || isDecimal(orginal);
    }


    /**
     * 是否是小数
     *
     * @param orginal
     * @return
     */
    public static boolean isDecimal(Double orginal) {
        double eps = 1e-10;
        boolean isInteger = orginal - Math.floor(orginal) < eps;
        return isInteger;
    }


    public static void main(String[] args) {
        Double a = 1D;
        boolean wholeNumber = NumberValidationUtils.isIntegerAndDecimal(String.valueOf(a));
        System.out.println(wholeNumber);
    }
}
